<template>
	<div class="app-container">
		<el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
			<el-form-item label="uuid：" prop="uuid">
				<el-input v-model="queryParams.uuid" placeholder="请输入uuid" clearable size="small" />
			</el-form-item>
			<el-form-item label="姓名：" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入姓名" clearable size="small" />
			</el-form-item>
			<el-form-item label="电话：" prop="phone">
				<el-input v-model="queryParams.phone" placeholder="请输入电话" clearable size="small" />
			</el-form-item>
			<el-form-item label="昵称：" prop="nickName">
				<el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable size="small" />
			</el-form-item>
			<el-form-item label="头像：" prop="avatar">
				<el-input v-model="queryParams.avatar" placeholder="请输入头像" clearable size="small" />
			</el-form-item>
			<el-form-item label="邮箱：" prop="email">
				<el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable size="small" />
			</el-form-item>
			<el-form-item label="详细地址：" prop="address">
				<el-input v-model="queryParams.address" placeholder="请输入详细地址" clearable size="small" />
			</el-form-item>
			<el-form-item label="生日：" prop="birthday">
				<el-date-picker v-model="queryParams.birthday" clearable size="small" type="date" value-format="yyyy-MM-dd" placeholder="选择生日"> </el-date-picker>
			</el-form-item>
			<el-form-item label="推荐人uuid：" prop="referrerUuid">
				<el-input v-model="queryParams.referrerUuid" placeholder="请输入推荐人uuid" clearable size="small" />
			</el-form-item>
			<el-form-item label="身份证号：" prop="idCard">
				<el-input v-model="queryParams.idCard" placeholder="请输入身份证号" clearable size="small" />
			</el-form-item>
			<el-form-item label="性别对应SEX枚举类：" prop="sex">
				<el-select v-model="queryParams.sex" placeholder="请选择性别对应SEX枚举类" clearable size="small">
					<el-option label="请选择字典生成" value="" />
				</el-select>
			</el-form-item>
			<el-form-item label="密码：" prop="password">
				<el-input v-model="queryParams.password" placeholder="请输入密码" clearable size="small" />
			</el-form-item>
			<el-form-item label="租户ID：" prop="tenantId">
				<el-input v-model="queryParams.tenantId" placeholder="请输入租户ID" clearable size="small" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button v-hasPermi="['system:patient:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermi="['system:patient:edit']" type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermi="['system:patient:remove']" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermi="['system:patient:export']" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
			</el-col>
			<right-toolbar :show-search.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="patientList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="租户ID" align="center" prop="id" />
			<el-table-column label="uuid" align="center" prop="uuid" />
			<el-table-column label="姓名" align="center" prop="name" />
			<el-table-column label="电话" align="center" prop="phone" />
			<el-table-column label="昵称" align="center" prop="nickName" />
			<el-table-column label="头像" align="center" prop="avatar" />
			<el-table-column label="邮箱" align="center" prop="email" />
			<el-table-column label="详细地址" align="center" prop="address" />
			<el-table-column label="生日" align="center" prop="birthday" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="推荐人uuid" align="center" prop="referrerUuid" />
			<el-table-column label="身份证号" align="center" prop="idCard" />
			<el-table-column label="性别对应SEX枚举类" align="center" prop="sex" />
			<el-table-column label="密码" align="center" prop="password" />
			<el-table-column label="租户ID" align="center" prop="tenantId" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button v-hasPermi="['system:patient:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
					<el-button v-hasPermi="['system:patient:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或编辑患者信息对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="uuid：" prop="uuid">
					<el-input v-model="form.uuid" placeholder="请输入uuid" />
				</el-form-item>
				<el-form-item label="姓名：" prop="name">
					<el-input v-model="form.name" placeholder="请输入姓名" />
				</el-form-item>
				<el-form-item label="电话：" prop="phone">
					<el-input v-model="form.phone" placeholder="请输入电话" />
				</el-form-item>
				<el-form-item label="昵称：" prop="nickName">
					<el-input v-model="form.nickName" placeholder="请输入昵称" />
				</el-form-item>
				<el-form-item label="头像：" prop="avatar">
					<el-input v-model="form.avatar" placeholder="请输入头像" />
				</el-form-item>
				<el-form-item label="邮箱：" prop="email">
					<el-input v-model="form.email" placeholder="请输入邮箱" />
				</el-form-item>
				<el-form-item label="详细地址：" prop="address">
					<el-input v-model="form.address" placeholder="请输入详细地址" />
				</el-form-item>
				<el-form-item label="生日：" prop="birthday">
					<el-date-picker v-model="form.birthday" clearable size="small" type="date" value-format="yyyy-MM-dd" placeholder="选择生日"> </el-date-picker>
				</el-form-item>
				<el-form-item label="推荐人uuid：" prop="referrerUuid">
					<el-input v-model="form.referrerUuid" placeholder="请输入推荐人uuid" />
				</el-form-item>
				<el-form-item label="身份证号：" prop="idCard">
					<el-input v-model="form.idCard" placeholder="请输入身份证号" />
				</el-form-item>
				<el-form-item label="性别对应SEX枚举类：" prop="sex">
					<el-select v-model="form.sex" placeholder="请选择性别对应SEX枚举类">
						<el-option label="请选择字典生成" value="" />
					</el-select>
				</el-form-item>
				<el-form-item label="密码：" prop="password">
					<el-input v-model="form.password" placeholder="请输入密码" />
				</el-form-item>
				<el-form-item label="删除标志：" prop="delFlag">
					<el-input v-model="form.delFlag" placeholder="请输入删除标志" />
				</el-form-item>
				<el-form-item label="租户ID：" prop="tenantId">
					<el-input v-model="form.tenantId" placeholder="请输入租户ID" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listPatient, getPatient, delPatient, addPatient, updatePatient, exportPatient } from '@/api/account/patient';

export default {
	name: 'Patient',
	components: {},
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 患者信息表格数据
			patientList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 搜索参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				uuid: null,
				name: null,
				phone: null,
				nickName: null,
				avatar: null,
				email: null,
				address: null,
				birthday: null,
				referrerUuid: null,
				idCard: null,
				sex: null,
				password: null,
				tenantId: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				uuid: [{ required: true, message: 'uuid不能为空', trigger: 'blur' }],
			},
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 搜索患者信息列表 */
		getList() {
			this.loading = true;
			listPatient(this.queryParams).then((response) => {
				this.patientList = response.records;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				uuid: null,
				name: null,
				phone: null,
				nickName: null,
				avatar: null,
				email: null,
				address: null,
				birthday: null,
				referrerUuid: null,
				idCard: null,
				sex: null,
				password: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				tenantId: null,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id);
			this.single = selection.length !== 1;
			this.multiple = !selection.length;
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加患者信息';
		},
		/** 编辑按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id || this.ids;
			getPatient(id).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '编辑患者信息';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updatePatient(this.form).then((response) => {
							this.msgSuccess('编辑成功');
							this.open = false;
							this.getList();
						});
					} else {
						addPatient(this.form).then((response) => {
							this.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$confirm('是否确认删除患者信息编号为"' + ids + '"的数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delPatient(ids);
				})
				.then(() => {
					this.getList();
					this.msgSuccess('删除成功');
				});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有患者信息数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return exportPatient(queryParams);
				})
				.then((response) => {
					this.download(response.msg);
				});
		},
	},
};
</script>
